package br.com.naotebuk.controller;


import br.com.naotebuk.dao.UsuarioDao;
import br.com.naotebuk.modelo.Usuario;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author giovanni
 */
@Controller
public class LoginController {
	
	private final UsuarioDao dao;
	
	@Autowired
	public LoginController(UsuarioDao dao){
		this.dao = dao;
	}
	
	@RequestMapping("loginForm")
	public String loginForm(){
		return "login";
	}
	
	@RequestMapping("efetuaLogin")
	public String efetuaLogin(Usuario usuario, HttpSession session, Model model){
            try{
                session.setAttribute("usuarioLogado", dao.validaUsuario(usuario));
                return "index";
            }catch (RuntimeException e){
                model.addAttribute("erroLogin", true);
		return "login";
            }
	}
	
	@RequestMapping("logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:loginForm";
	}
	
}